package com.qk.filter;

import com.qk.utils.JwtUtils;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
//@WebFilter(urlPatterns = "/*")
public class TokenFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String url = request.getRequestURI();
        if(url.contains("/login")){
            filterChain.doFilter(request,response);
            return;
        }
        String token = request.getHeader("token");
        if (token == null){
           response.setStatus(401);
            return;
        }
        try{
            JwtUtils.parseToken(token);
        }catch (Exception e){
            response.setStatus(401);
           return;
        }
        filterChain.doFilter(request,response);
    }
}
